// NFS 挂载相关操作
import { CleanAllNfsDocument, NfsMountInstanceResourcesDocument } from '@/generated/graphql'
import { Client } from '@urql/vue'
import { defineStore } from 'pinia'

interface NFSState {
  documentId?: string
  instanceId?: string // openstack实例ID
}

export const useNFS = defineStore({
  id: 'nfs',
  persist: {
    storage: sessionStorage
  },
  state: (): NFSState => ({
    documentId: undefined,
    instanceId: undefined
  }),
  actions: {
    setNFSState(arg: NFSState) {
      const { documentId, instanceId } = arg
      this.$patch({
        documentId,
        instanceId
      })
    },
    // 解除挂载
    async mountedNFS(client: Client) {
      if (!this.documentId || !this.instanceId) {
        return Promise.reject('documentId or instanceId error!')
      }
      const that = this
      return await client
        .mutation(NfsMountInstanceResourcesDocument, {
          data: {
            instanceId: that.instanceId,
            id: that.documentId
          }
        })
        .toPromise()
    },
    //解除所有挂载
    async unMountedNFSAll(client: Client) {
      if (!this.documentId || !this.instanceId) {
        return Promise.reject('instanceId error!')
      }
      const that = this
      //我改后:前端不需要nfs操作
      // return await client
      //   .mutation(CleanAllNfsDocument, {
      //     data: {
      //       id: that.instanceId
      //     }
      //   })
      //   .toPromise()
    }
  }
})
